<?php
/**
 * Creater: 李青林
 * Date Time : 2018/9/26 10:26
 */

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\UserCapital;
use App\Http\Models\User;
use App\Http\Models\RechargeLog;

class UserFinanceController extends Controller
{
    use ModelForm;
    protected $headerStr = '点券';
    protected $userid;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->description('点券流水');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return Admin::grid(UserCapital::class, function (Grid $grid) {
            $grid->model()->select('users_capital.id', 'users.name', 'users.avatar', 'users_capital.voucher_number', 'users_capital.user_id','users_capital.created_at'
            )->leftjoin('users', 'users.id', '=', 'users_capital.user_id');
            $grid->id('ID');
            $grid->user_id('用户ID(昵称)')->display(function () {
                return $this->user_id . '(' . $this->name . ')';
            })->sortable();
            $grid->avatar('头像')->display(function ($avatar) {
                return user_image_url($avatar);
            })->image(40, 40);
            $grid->voucher_number('余额')->sortable();
            $grid->created_at('时间')->sortable();
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->paginate(env('Admin_Limit'));
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->like('name', '用户昵称');
                $filter->equal('user_id', '用户ID');
            });
            //$grid->disableActions();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                if(method_exists($actions,'disableView')){
                    $actions->disableView();
                }
                $actions->disableEdit();
                $actions->append("<a href='/admin/userfinance/create?uid={$actions->row['user_id']}'><i class='fa fa-edit'></i></a>");
            });
        });
    }

    public function create()
    {
        $this->userid = request('uid');
        if (empty($this->userid)) {
            return back();
        }
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }

    protected function form()
    {
        return Admin::form(RechargeLog::class, function (Form $form) {
            $form->display('user_id', '用户id:')->default($this->userid);
            $form->hidden('user_id', '用户id:')->default($this->userid);
            $form->hidden('type')->default(0);
            $form->number('money', '充值数量:')->max(100000000)->min(1)->rules('required|regex:/^\d+$/|min:2', [
                'regex' => 'code必须全部为数字',
                'min' => 'code不能少于2个字符',
            ]);
            $form->disableReset();
            $form->radio('status', '充值/扣除:')->options(['1' => '充值', '0' => '扣除']);
            $form->text('content', '描述:')->placeholder('请说明充值或扣款原因')->rules('required|min:3');
            // $form->setWidth(3, 2);
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
            $form->saving(function (Form $form) {
                $staus = $form->status == 1 ? 2 : 3;
                UserCapital::addUserCapital(request('user_id'), request('money'), $staus, request('content'));
            });

        });
    }

}